﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Reflection;
using System.Xml;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using System.IO;
using Cn.Edwon.Web;
using Cn.Edwon.Web.Support;
using Cn.Cellula.Client;

namespace Cn.Cellula.Website.Admin.Site {
    public partial class BaseConfig : PageSupport {
        IUserBiz userBiz = BizFactory.Get<IUserBiz>();
        XmlDocument doc = new XmlDocument();
        String cfgFilePath = ClientHelper.RootPath + "web.config";
        protected void Page_Load(object sender, EventArgs e) {
            doc.Load(cfgFilePath);
            if (!Page.IsPostBack) {
                InitForm();
                FillForm();
            }
        }
        void InitForm() {
        }
        void FillForm() {
            this.txt_Copyright.Text = ClientBiz.GetAppItem(ref doc,AppSettingsKey.SysCopyright);
            this.txt_SiteName.Text = ClientBiz.GetAppItem(ref doc,AppSettingsKey.SysSiteName);
            this.txt_SiteTitle.Text = ClientBiz.GetAppItem(ref doc,AppSettingsKey.SysPageTitle);
            this.img_Preview.ImageUrl = ClientBiz.GetAppItem(ref doc, AppSettingsKey.SysLogoUrl);
        }

        protected void btn_save_Click(object sender, EventArgs e) {
            try {
                File.Copy(cfgFilePath, cfgFilePath + ".bak", true);
            } catch (Exception ex) {
                log.Error(ex.Message);
                SiteInfo.Send(Page, TxtDef.ERROR, "备份配置文件时错误");
                return;
            }
            try {
                ClientBiz.SetAppItem(ref doc,AppSettingsKey.SysCopyright, this.txt_Copyright.Text);
                ClientBiz.SetAppItem(ref doc, AppSettingsKey.SysSiteName, this.txt_SiteName.Text);
                ClientBiz.SetAppItem(ref doc, AppSettingsKey.SysPageTitle, this.txt_SiteTitle.Text);
                try {
                    if (!String.IsNullOrEmpty(this.file_Logo.FileName)) {
                        string extName = Path.GetExtension(this.file_Logo.FileName);
                        if (!Directory.Exists(SiteConfig.Upload.UploadPath + "site/")) {
                            Directory.CreateDirectory(SiteConfig.Upload.UploadPath + "site/");
                        }
                        this.file_Logo.SaveAs(SiteConfig.Upload.UploadPath + "site/logo" + extName);
                        ClientBiz.SetAppItem(ref doc, AppSettingsKey.SysLogoUrl, SiteConfig.Upload.UploadForder + "site/logo" + extName);
                    }
                } catch (Exception ex) {
                    log.Error(ex.Message);
                }
                doc.Save(cfgFilePath);
                BizFactory.Get<ILogBiz>().Info("进行了网站基本信息设置操作", this.Actor, Request);
                SiteInfo.SendSucceed(Page, "");
            } catch (Exception ex) {
                log.Error(ex.Message);
                SiteInfo.Send(Page,TxtDef.ERROR,"保存配置项时出现错误，保存失败");
            }
        }
    }
}
